
#include "bluso_lib.h"


section* find_section( char* section )
{
    int i;
    
    for( i = 0; i < 32 || g_bluso_config[i] != None ; i++ )
    {
        if ( strcmp( g_bluso_config[i].section_name, section ) == 0 )
        {
            return g_bluso_config + i;
        }
    }
    
    return NULL;
}

int find_option( section* sec, char* option )
{
    int i;
    
    for( i = 0; i < sec->option_count; i++ )
    {
        if ( strcmp( sec->option_name[i], option ) == 0 )
        {
            return i;
        }
    }
    
    return -1;
}


int set_config_number( char* section , char* option, long inp )
{
    section* s;
    int i;
    
    s = find_section( section );
    
    if ( s == NULL ) return -1;
    
    i = find_option( s, option );
    
    if ( i < 0 ) return -2;
    
    if ( s->types[i] != pt_number ) return -3;
    
    (long)*( s->st_config + s->offset[i] ) = inp;
    
    return 0;
}

int set_config_float( char* section, char* option, double inp )
{
    section* s;
    int i;
    
    s = find_section( section );
    
    if ( s == NULL ) return -1;
    
    i = find_option( s, option );
    
    if ( i < 0 ) return -2;
    
    if ( s->types[i] != pt_number ) return -3;
    
    (double)*( s->st_config + s->offset[i] ) = inp;
    
    return 0;
}


int set_config_string( char* section, char* option, char* inp )
{
    section* s;
    int i;
    
    s = find_section( section );
    
    if ( s == NULL ) return -1;
    
    i = find_option( s, option );
    
    if ( i < 0 ) return -2;
    
    if ( s->types[i] != pt_number ) return -3;
    
    strcpy( (char *)( s->st_config + s->offset[i] ) , inp );
    
    return 0;
}
